import requests
TEST_DOMAIN = 'https://plat-test.recsteel.cn/api'
FORMAL_DOMAIN = 'https://gtp.recsteel.cn/api'
TEST_TOKEN='eyJjb21wYW55SWQiOjEsInVwZGF0ZURhdGUiOjE3NDY2ODQzMDAyMzQsIm9wZXJhdGVJZCI6MTMsImNvbXBhbnlOYW1lIjoi6ams6Z6N5bGx6ZKi5Zue5a625pyJ6ZmQ6LSj5Lu75YWs5Y-4IiwiZGVwYXJ0SWQiOjIsIm9wZXJhdGVOYW1lIjoi6LaF566hIiwic3VwZXJBZG1pbiI6dHJ1ZSwicGxhdGZvcm0iOiJ3ZWIiLCJjcmVhdGVEYXRlIjoxNzQ2Njg0MzAwMjM0LCJkZXBhcnROYW1lIjoi6L-Q6JCl566h55CG6YOoIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiIxMyIsImlzcyI6IlhQVFpAR0hKIiwiaWF0IjoxNzQ2Njg0MzAwLCJleHAiOjE3NDY4NTcxMDB9.zZqc67Hw1lB-JIxSSHaLm7vABsuTgTAzi57PAdd8k20'
FORMAL_TOKEN='eyJjb21wYW55SWQiOjEsInVwZGF0ZURhdGUiOjE3NDY2ODQwNTY2ODMsIm9wZXJhdGVJZCI6MSwiY29tcGFueU5hbWUiOiLpqazpno3lsbHpkqLlm57lrrbmnInpmZDotKPku7vlhazlj7giLCJkZXBhcnRJZCI6Miwib3BlcmF0ZU5hbWUiOiLotoXnrqEiLCJzdXBlckFkbWluIjp0cnVlLCJwbGF0Zm9ybSI6IndlYiIsImNyZWF0ZURhdGUiOjE3NDY2ODQwNTY2ODMsImRlcGFydE5hbWUiOiLov5DokKXnrqHnkIbpg6giLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxIiwiaXNzIjoiWFBUWkBHSEoiLCJpYXQiOjE3NDY2ODQwNTYsImV4cCI6MTc0Njg1Njg1Nn0.NAe-8tRwZvs-7QNtxhNl4FirnNgJvJ0Ik9yj0_AZ7Rs'
def make_request(method, url, data={}, mode=None):
    params = None
    json = None
    if method == 'GET':
        params = data
    else:
        json = data
    headers = {}
    if mode == 'test':
        headers['authorization'] = TEST_TOKEN
        url = TEST_DOMAIN + url
    else:
        headers['authorization'] = FORMAL_TOKEN
        url = FORMAL_DOMAIN + url
    try:
        response = requests.request(method, url, params=params, json=json, headers=headers)
        if response.status_code == 200:
            return response.json()['data']  # 假设响应内容为 JSON 格式
        else:
            return f"{method} request failed with status code {response.status_code}"
    except requests.exceptions.RequestException as e:
        return f"{method} request failed: {e}"

# 示例调用
menuApiList = lambda data,mode:make_request("POST", "/basic/p/menuApiSearch", data,mode)
menuList = lambda data,mode:make_request("POST", "/basic/p/menuList", data,mode)
menuAdd = lambda data,mode:make_request("POST", "/basic/p/saveMenu", data,mode)
menuApiSave = lambda data,mode:make_request("POST", "/basic/p/saveMenuApi", data,mode)


apiList = lambda data,mode:make_request("POST", "/basic/p/apiList", data,mode)
apiAdd = lambda data,mode:make_request("POST", "/basic/p/saveApi", data,mode)
def syncApi():
    testApiiList = apiList({"pageInfo":{"pageNum": 1, "pageSize": 9999}},"test")
    formalApiiList = apiList({"pageInfo":{"pageNum": 1, "pageSize": 9999}},"formal")
    formalApiiDict = {item["apiPath"]: item for item in formalApiiList}
    for item in testApiiList:
        apiPath = item["apiPath"]
        if apiPath not in formalApiiDict:
            apiAdd({"apiDesc":item["apiDesc"],"apiNo":item["apiNo"],"apiPath":item["apiPath"]},"formal")


def syncMenu():
    testMenuList = menuList({},"test")
    formalMenuList = menuList({},"formal")
    formalApiiList = apiList({"pageInfo":{"pageNum": 1, "pageSize": 9999}},"formal")
    formalApiiDict = {item["apiPath"]: item for item in formalApiiList}
    
    def syncMenus(testMenuChildren,formalMenuChildren,parentId):
        if testMenuChildren is None:
            return
        print(testMenuChildren)
        testMenuChildrenDict = {item["menuName"]: item for item in testMenuChildren}
        if formalMenuChildren is not None:
            formalMenuChildrenDict = {item["menuName"]: item for item in formalMenuChildren}
        else:
            formalMenuChildrenDict = {}
        for menuName in testMenuChildrenDict:
            print(menuName)
            testMenu = testMenuChildrenDict[menuName]
            if menuName not in formalMenuChildrenDict:
                formalMenuId = menuAdd({
                    "menuParentId": parentId,
                    "menuName": testMenu["menuName"],
                    "menuUrl": testMenu["menuUrl"],
                    "menuNo": testMenu["menuNo"],
                    "menuType": testMenu["menuType"],
                    "menuAppType": testMenu["menuAppType"],
                    "menuLevel": testMenu["menuLevel"],
                    "menuIcon": testMenu["menuIcon"],
                    "isVisible": testMenu["isVisible"],
                    "superAdmin": testMenu["superAdmin"],
                    "sort": testMenu["sort"],
                    "extend1": testMenu["extend1"]
                },'formal')
                formalMenu = {
                    "id":formalMenuId,
                    "children":[]
                }
            else:
                formalMenu = formalMenuChildrenDict[menuName]
            # testMenuApiList = menuApiList({"menuId":testMenu["id"]},"test")
            # formalMenuApiList = menuApiList({"menuId":formalMenu["id"]},"formal")
            # if formalMenuApiList is not None:
            #     formalMenuApiDict = {item["apiPath"]: item for item in formalMenuApiList}
            #     formalMenuApiIdList = [item["id"] for item in formalMenuApiList]
            # else:
            #     formalMenuApiDict = []
            #     formalMenuApiIdList = []
            # hasAdd = False
            # for item in testMenuApiList:
            #     apiPath = item["apiPath"]
            #     if apiPath not in formalMenuApiDict:
            #         hasAdd = True
            #         formalMenuApiIdList.append(formalApiiDict[apiPath]["id"])
            # if hasAdd:
            #     menuApiSave({
            #         "apiIds":formalMenuApiIdList,
            #         "menuId":formalMenu["id"]
            #     },"formal")
            if testMenu["children"] and len(testMenu["children"])>0:
                syncMenus(testMenu["children"],formalMenu["children"],formalMenu["id"])
    syncMenus(testMenuList[1]["children"],formalMenuList[1]["children"],0)
# syncApi()
syncMenu()
